/**
 * JAVACC DEMO 1.0
 * @copy right dwusoft company All rights reserved. 
 * @Package com.apache.bid.service.cacheplugins.ws  
 */
package com.apache.bid.service.cacheplugins.ws;

import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;

import com.apache.api.manager.PluginConnector;
import com.apache.api.manager.ProxyManager;
import com.apache.api.vo.ParamsVo;
import com.apache.api.vo.ResultEntity;
import com.apache.bid.common.Constant;
import com.apache.bid.common.cache.BidCacheFactory;
import com.apache.bid.common.cache.BidCacheManager;
import com.apache.bid.entity.BidItem;
import com.apache.bid.entity.BidSession;
import com.apache.bid.manager.BidSessionManager;
import com.apache.cache.util.Validator;
import com.apache.database.constant.SystemTools;
import com.apache.tools.DateUtils;

/**
 * description:  同步标的信息
 * @author dyhou 创建时间：2016-6-6  
 */
public class BidItemSynPluginImpl implements PluginConnector {

	private ProxyManager wbdProxyCacheManager;//cacheSaveAndStartItemPlugin
	private BidSessionManager bidSessionManager;
	private Logger log = Logger.getLogger(getClass());

	/**
	 * TODO 简单描述该方法的实现功能（可选）.  
	 * @see com.apache.api.manager.PluginConnector#execute(com.apache.api.vo.ParamsVo)  
	 */
	public Object execute(ParamsVo param) throws Exception {
		String itemIds = String.valueOf(param.getParams("itemIds"));
		ResultEntity entity = new ResultEntity();
		if (Validator.isNotNull(itemIds)) {//同步ID在查业务系统中标的表

		} else {
			List<Map<String, String>> items = (List<Map<String, String>>) param.getParams("items");
			if (Validator.isEmpty(items)) {
				entity.setResult("false");
				entity.setMessage("标的信息为空");
				return entity;
			}
			String sessionId = Validator.getDefaultStr(String.valueOf(param.getParams("sessionid")), "0");
			log.info("推送标的sessionId=" + sessionId);
			BidSession session = (BidSession) BidCacheFactory.getInstance().getCacheManager("session")
					.getCacheCloneByKey(BidCacheManager.session_prefix + sessionId);
			if (Validator.isEmpty(session) && !"0".equals(sessionId)) {
				ParamsVo vo = new ParamsVo();
				vo.setInfoId(sessionId);
				session = (BidSession) bidSessionManager.getInfoById(vo);
			}
			for (int i = 0; i < items.size(); i++) {
				saveItemInfo(items.get(i));
			}
		}
		entity.setResult("true");
		entity.setEntity("true");
		entity.setMessage("标的信息同步成功");
		return entity;
	}

	private void saveItemInfo(Map<String, String> map) {
		ParamsVo<BidItem> vo = new ParamsVo<BidItem>();
		try {
			BidItem item = new BidItem();
			item.setItemId(map.get("itemid"));
			String sessionId = Validator.getDefaultStr(map.get("sessionid"), "0");
			item.setSessionId(sessionId);
			if ("0".equals(sessionId)) {
				item.setSessionType(Constant.SETYPE_HD);
			} else {
				BidSession session = (BidSession) BidCacheFactory.getInstance().getCacheManager("session")
						.getCacheCloneByKey(BidCacheManager.session_prefix + sessionId);
				if (null != session) {
					item.setSessionType(session.getSessionType());
					item.setSessionName(session.getSessionName());
				}
			}
			item.setItemNo(map.get("itemno"));
			item.setItemName(map.get("itemname"));
			item.setItemNum(map.get("itemnum"));
			item.setBidMode(map.get("bidmode"));
			item.setFirstValuePre(map.get("firstvalue"));
			item.setAllowFirstValue(map.get("allowFvalue"));
			item.setHoldvalue(map.get("holdvalue"));
			item.setBidWay(map.get("bidway"));
			item.setFlagTime(map.get("flagtime"));
			item.setCreateTime(Validator.getDefaultStr(map.get("createtime"), DateUtils.Now.fmt_yyyyMMdd_HHmmss()));
			item.setItemType(map.get("type"));
			item.setIncrementValue(map.get("incrementValue"));
			item.setIncrementGroup(map.get("incrementGroup"));
			item.setLimitTime(map.get("limittime"));
			item.setCloseTimePre(map.get("closetime"));
			item.setOpenTimePre(map.get("opentime"));
			item.setFirstBidTimePre(map.get("firstbidtime"));
			item.setOncevalue(map.get("oncevalue"));
			item.setOrderNumber(map.get("orderNumber"));
			item.setOrgEname(map.get("spaceEname"));
			item.setOrgCname(map.get("orgCname"));
			item.setEndPrioDays(map.get("endPrioDays"));
			item.setAllowEndPrio(map.get("allowEndPrio"));
			item.setAllowHallPrio(map.get("allowHallPrio"));
			item.setAllowAddTime(map.get("addBidMinutes"));
			item.setAllowAdd(map.get("allowEndBid"));
			item.setItemStatus("2");
			item.setCurrency(map.get("currency"));
			item.setPriceUnit(map.get("monunit"));
			item.setPriceFlag(map.get("priceFlag"));
			item.setItemUnitName(map.get("ton"));
			item.setDecimalCount(Validator.getDefaultStr(map.get("decimalcount"), "2"));
			item.setItemTypeName(map.get("itemType2"));
			item.setItemArea(map.get("itemArea"));
			item.setAllowRepeat(map.get("allowRepeat"));
			item.setHoldNoticeTime(map.get("holdNoticeTime"));
			item.setBidInterval(map.get("bidInterval"));
			item.setBidIntervalLimit(map.get("bidIntervalLimit"));
			item.setItemNumDesc(map.get("itemNumDesc"));
			item.setIfFee(map.get("iscommission"));//是否收取服务费
			item.setBailPrice(Validator.getDefaultStr(map.get("bail"), "0"));
			item.setDelayControl(map.get("delayControl"));
			item.setDelayPeriod(map.get("delayPeriod"));
			item.setDelayDeadTime(map.get("delayDeadTime"));
			item.setDelayUnit(map.get("delayUnit"));
			item.setFallDecrement(map.get("decrement"));
			item.setFallLowerstPrice(map.get("lowerstPrice"));
			item.setFallLimitTime(map.get("limitfalltime"));
			item.setPicUrl(map.get("picurl"));
			item.setPrioControl(map.get("prioControl"));
			item.setProId(map.get("packageId"));
			item.setPackId(map.get("afterTimeEnd"));
			vo.setObj(item);
			vo.setKey("cacheSaveAndStartItemPlugin");
			if ("0".equals(item.getSessionId())) {
				vo.setParams("bjServer", SystemTools.getInstance().getValue("wbd_cache_url"));
				vo.setParams("bjPort", SystemTools.getInstance().getValue("wbd_cache_port"));
			}
			wbdProxyCacheManager.saveInvoke(vo);
		} catch (Exception e) {
			log.error("同步标的[" + map.get("itemname") + "]失败");
		}
	}

	public void setWbdProxyCacheManager(ProxyManager wbdProxyCacheManager) {
		this.wbdProxyCacheManager = wbdProxyCacheManager;
	}

	public void setBidSessionManager(BidSessionManager bidSessionManager) {
		this.bidSessionManager = bidSessionManager;
	}
}
